package com.lagou.rabbitmq.demo;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.URI;

@Configuration
public class RabbitConfig {

    //连接工厂
    @Bean
    public ConnectionFactory connectionFactory(){
        ConnectionFactory connectionFactory = new CachingConnectionFactory(URI.create("amqp://root:123456@47.115.74.219:5672/%2f"));
        return connectionFactory;
    }

    //RabbitTemplate
    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory){
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);

        return rabbitTemplate;
    }

    //RabbitAdmin
    @Bean
    public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory){
        RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);

        return rabbitAdmin;
    }


    //Queue
    @Bean
    public Queue queue(){
        Queue queue = QueueBuilder.nonDurable("queue.anno").build();
        return queue;
    }

    //Exchage
    @Bean
    public Exchange exchange(){
        FanoutExchange fanoutExchange = new FanoutExchange("ex.anno.fanout", false, false, null);
        return fanoutExchange;
    }

    //Binding
    @Bean
    public Binding binding(Queue queue,Exchange exchange){
        Binding binding = BindingBuilder.bind(queue).to(exchange).with("key.anno").noargs();

        return binding;
    }


}
